package org.opsli.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;

@Slf4j
@Component
public class DYCUtils {

    /**
     * 从ids中获取id
     * @param orgIds
     * @return
     */
    public static String getShopId(String orgIds){
        String id = "0";
        if(orgIds != null && orgIds.length() > 1){
            String[] ids = orgIds.split(",");
            id = ids[1];
        }
        return id;
    }


    /**
     * 从ids中获取id
     * @param orgIds
     * @return
     */
    public static String getOrgIdByOrgIds(String orgIds){
        if(orgIds==null){
            new RuntimeException("orgIds 不能为空");
        }
       if (orgIds.split(",").length==1){

       }

        if (orgIds.split(",").length>1&& orgIds.split(",")[0].equals("0")){
            orgIds = orgIds.split(",")[1];
        }

       return orgIds;
    }



    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        DYCUtils.redisTemplate = redisTemplate;
    }
    /**
     *  设置redis缓存
     */
    private static RedisTemplate<String, Object> redisTemplate;

    // 设置缓存
    public static void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 设置缓存并指定过期时间
    public static void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 获取缓存
    public static Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除缓存
    public static void delete(String key) {
        redisTemplate.delete(key);
    }

    // 判断缓存是否存在
    public static boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }
}
